#include "head.h"

// void obj_pos(){
//     lv_obj_t *screen = lv_scr_act();

//     // 生成12个随机十六进制颜色
//     uint32_t colors[12];
//     srand(time(NULL)); // 初始化随机种子

//     for(int i = 0; i < 12; i++) {
//         // 生成0x000000到0xFFFFFF之间的随机颜色
//         // 避免过暗颜色：限制RGB值最低为0x30（48）
//         uint8_t r = 0x30 + rand() % (0xFF - 0x30);
//         uint8_t g = 0x30 + rand() % (0xFF - 0x30);
//         uint8_t b = 0x30 + rand() % (0xFF - 0x30);
        
//         colors[i] = (r << 16) | (g << 8) | b;
//     }

//     // 可选：使用洗牌算法增加随机性
//     for(int i = 11; i > 0; i--) {
//         int j = rand() % (i + 1);
//         uint32_t temp = colors[i];
//         colors[i] = colors[j];
//         colors[j] = temp;
//     }

//     srand(time(NULL));
//     for(int i = 0; i < 3; i++) {
//         for(int j = 0; j < 4; j++) {
//         lv_obj_t *obj = lv_obj_create(screen);
//         lv_obj_set_style_bg_color(obj, lv_color_hex(colors[4*i+j]), 0);
//         lv_obj_set_style_bg_color(obj, lv_color_hex(colors[4*i+j]), LV_PART_MAIN|LV_STATE_PRESSED);
//         lv_obj_set_size(obj, 200, 160);
//         lv_obj_align(obj, LV_ALIGN_TOP_LEFT, 200*j, 160*i);
//         }
//     }
// }

void obj_style(){
    /*A base style*/
    static lv_style_t style_base;
    lv_style_init(&style_base);//初始化
    lv_style_set_bg_color(&style_base, lv_palette_main(LV_PALETTE_LIGHT_GREEN));//设置背景颜色
    lv_style_set_border_color(&style_base, lv_palette_darken(LV_PALETTE_LIGHT_GREEN, 3));//设置边框颜色
    lv_style_set_border_width(&style_base, 5);//设置边框宽度
    lv_style_set_radius(&style_base, 10);//设置圆角
    lv_style_set_shadow_width(&style_base, 10);//设置阴影宽度
    lv_style_set_shadow_ofs_y(&style_base, 5);//设置纵向阴影偏移量
    lv_style_set_shadow_ofs_x(&style_base, 5);//设置横向阴影偏移量
    lv_style_set_shadow_opa(&style_base, LV_OPA_50);//设置阴影透明度
    lv_style_set_text_color(&style_base, lv_color_black());//设置文本颜色
    lv_style_set_text_letter_space(&style_base, 2);//设置文本间距
    lv_style_set_width(&style_base, 300);//设置宽度
    lv_style_set_height(&style_base, 300);//设置高度

    lv_obj_t * obj_base = lv_obj_create(lv_scr_act());
    lv_obj_add_style(obj_base, &style_base, 0);
    lv_obj_set_style_bg_color(obj_base, lv_palette_main(LV_PALETTE_ORANGE), 0);
    lv_obj_align(obj_base, LV_ALIGN_CENTER, 0, 0);

    lv_obj_t * label = lv_label_create(obj_base);
    lv_label_set_text(label, "Base");
    lv_obj_center(label);
}

void obj_transition(){
    // 数组props，用于指定背景色、边框色和边框宽度三个属性将在过渡期间被修改
    static const lv_style_prop_t props[] = {LV_STYLE_BG_COLOR, LV_STYLE_BORDER_COLOR, LV_STYLE_BORDER_WIDTH, 0};

    // 过渡持续时间（100毫秒），即过渡动画完成所需的时间
    // 延迟时间（200毫秒），表示在状态改变后，过渡动画开始前需要等待的时间
    //lv_anim_path_linear 线性过渡
    //props 指定要过渡的属性
    static lv_style_transition_dsc_t trans_def;//开始的样式
    lv_style_transition_dsc_init(&trans_def, props, lv_anim_path_linear, 100, 200, NULL);

    /* A special transition when going to pressed state
    * Make it slow (500 ms) but start  without delay*/
    static lv_style_transition_dsc_t trans_pr;//最后的样式
    lv_style_transition_dsc_init(&trans_pr, props, lv_anim_path_linear, 1000, 200, NULL);

    static lv_style_t style_def;
    lv_style_init(&style_def);
    lv_style_set_bg_color(&style_def, lv_palette_lighten(LV_PALETTE_PINK, 3));
    lv_style_set_border_width(&style_def, 6);
    lv_style_set_border_color(&style_def, lv_palette_lighten(LV_PALETTE_PINK, 2));
    lv_style_set_transition(&style_def, &trans_def);

    static lv_style_t style_pr;
    lv_style_init(&style_pr);
    lv_style_set_bg_color(&style_pr, lv_palette_main(LV_PALETTE_RED));
    lv_style_set_border_width(&style_pr, 20);
    lv_style_set_border_color(&style_pr, lv_palette_darken(LV_PALETTE_RED, 3));
    lv_style_set_transition(&style_pr, &trans_pr);

    /*Create an object with the new style_pr*/
    lv_obj_t * obj = lv_obj_create(lv_scr_act());
    lv_obj_add_style(obj, &style_def, 0);
    lv_obj_add_style(obj, &style_pr, LV_STATE_PRESSED);

    lv_obj_center(obj);
}

static void btn_event_cb1(lv_event_t * e){
    LV_LOG_USER("Clicked");//日志输出函数
    static uint32_t count = 0;
    lv_obj_t * btn = lv_event_get_target(e);//获取事件的目标对象
    lv_obj_t * label = lv_obj_get_child(btn, 0);//获取按钮的第一个子对象，即标签
    lv_label_set_text_fmt(label, "%d", count);
    count++;
}

void btn_event1(){
    lv_obj_t * btn = lv_btn_create(lv_scr_act());
    lv_obj_set_size(btn, 100, 50);
    lv_obj_center(btn);
    lv_obj_add_event_cb(btn, btn_event_cb1, LV_EVENT_CLICKED, NULL);
    lv_obj_t * label = lv_label_create(btn);
    lv_label_set_text(label, "Click me");
    lv_obj_center(label);
}

static void slider_event_cb1(lv_event_t * e){
    lv_obj_t * slider = lv_event_get_target(e);
    int32_t value = lv_slider_get_value(slider);
    lv_obj_t * label = lv_event_get_user_data(e);
    lv_label_set_text_fmt(label, "%d%%", value);
    lv_obj_update_layout(label);
    lv_coord_t slider_x = lv_obj_get_x(slider);
    lv_coord_t slider_y = lv_obj_get_y(slider);
    lv_coord_t slider_w = lv_obj_get_width(slider);
    lv_coord_t label_x = slider_x + (float)value/lv_slider_get_max_value(slider)*slider_w;
    lv_coord_t label_y = slider_y - 30;
    lv_obj_set_pos(label, label_x - lv_obj_get_width(label)/2, label_y);
}

void slider_event1(){
    lv_obj_t * slider = lv_slider_create(lv_scr_act());
    lv_obj_align(slider, LV_ALIGN_CENTER, 0, 0);

    lv_obj_t * label = lv_label_create(lv_scr_act());
    lv_label_set_text(label, "0");
    lv_obj_set_style_bg_opa(label, LV_OPA_100, 0);
    lv_obj_set_style_bg_color(label, lv_palette_lighten(LV_PALETTE_GREY, 1), 0);
    lv_obj_set_style_border_color(label, lv_palette_lighten(LV_PALETTE_GREY, 2), 0);
    lv_obj_set_style_radius(label, 4, 0);

    lv_obj_align_to(label, slider, LV_ALIGN_BOTTOM_LEFT, 0, -30);

    lv_obj_add_event_cb(slider, slider_event_cb1, LV_EVENT_VALUE_CHANGED, label);
}

void show_fixed_img(const lv_img_dsc_t *img){
    lv_obj_t * img1 = lv_img_create(lv_scr_act());
    lv_img_set_src(img1, img);
    // 获取图片原始尺寸
    uint32_t img_w = img->header.w;
    uint32_t img_h = img->header.h;
    // 获取屏幕尺寸
    lv_obj_t * screen = lv_scr_act();
    uint32_t screen_w = lv_obj_get_width(screen);
    uint32_t screen_h = lv_obj_get_height(screen);
    // 计算宽度和高度方向的缩放比例（取较小值以确保完整显示）
    uint32_t scale_w = (screen_w * 100) / img_w;  // 宽度方向缩放百分比
    uint32_t scale_h = (screen_h * 100) / img_h;  // 高度方向缩放百分比
    uint32_t scale = LV_MIN(scale_w, scale_h) * 0.9;  // 乘以0.9留边距
    // 设置缩放比例（LVGL的缩放值以256为100%，所以需要转换）
    lv_img_set_zoom(img1, (scale * LV_IMG_ZOOM_NONE) / 100);
    lv_obj_align(img1, LV_ALIGN_CENTER, 0, 0);
}

void show_variable_img(char * file_name)
{
    lv_obj_t * img = lv_img_create(lv_scr_act());
    lv_img_set_src(img, file_name);
    lv_obj_align(img, LV_ALIGN_RIGHT_MID, 0, 0);
}